package org.hive.vmware.translator;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hive.vmware.model.VmBee;
import org.hive.vmware.model.VmCenterServer;
import org.hive.vmware.model.VmGroupBee;

public class VmGroupModelDiskPersistenceTranslator {

		
	public VmCenterServer translateVmCenterServerToModel(String centerserver) {
		VmCenterServer result = null;
		if (centerserver != null && centerserver.trim().length() > 0) {
			String[] strings = centerserver.split(",");
			if (strings.length == 3) {
				result = new VmCenterServer();
				result.setId(strings[0]);
				result.setUserId(strings[1]);
				result.setPassword(strings[2]);
			}
		}
		return result;
	}
	
	public String translateVmCenterServerToPersistence(VmCenterServer vmCenterServer) {
		StringBuilder builder = new StringBuilder();
		builder.append(vmCenterServer.getId());
		builder.append(",");
		builder.append(vmCenterServer.getUserId());
		builder.append(",");
		builder.append(vmCenterServer.getPassword());
		return builder.toString();
	}
	
	public List<String> translateVmGroupToPersistence(VmGroupBee vmGroup) {
		List<String> results = new ArrayList<String>();
		Set<VmBee> vms = vmGroup.getVms();
		for (VmBee vmBee : vms) {
			StringBuilder builder = new StringBuilder();
			builder.append(vmGroup.getId());
			builder.append(",");
			builder.append(translateVmToPersistence(vmBee));
			results.add(builder.toString());
		}
		return results;
	}
	
	public VmGroupBee translateVmGroupToModel(String vmGroupString) {
		VmGroupBee vmGroup = null;
		if (vmGroupString != null && vmGroupString.trim().length() > 0) {
			String[] strings = vmGroupString.split(",");
			if (strings.length == 4) {
				vmGroup = new VmGroupBee();
				vmGroup.setId(strings[0]);
				VmBee vmBee = new VmBee();
				vmBee.setName(strings[1]);
				vmBee.setHost(strings[2]);
				vmBee.setCenterServer(strings[3]);
				vmGroup.addVm(vmBee);
			}	
		}
		return vmGroup;
	}
	
	String translateVmToPersistence(VmBee vm) {
		StringBuilder builder = new StringBuilder();
		builder.append(vm.getName());
		builder.append(",");
		builder.append(vm.getHost());
		builder.append(",");
		builder.append(vm.getCenterServer());
		return builder.toString();
	}
	
	
}
